package com.sunbim.rsrv.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunbim.rsrv.common.exception.RequestException;
import com.sunbim.rsrv.mapper.RelicsMapper;
import com.sunbim.rsrv.params.risk.RiskStructParams;
import com.sunbim.rsrv.pojo.RiskManage;
import com.sunbim.rsrv.pojo.RiskStruct;
import com.sunbim.rsrv.mapper.RiskStructMapper;
import com.sunbim.rsrv.service.RelicsService;
import com.sunbim.rsrv.service.RiskStructService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunbim.rsrv.util.common.ProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 结构安全风险 服务实现类
 * </p>
 *
 * @author yuhui
 * @since 2021-12-30
 */
@Service
public class RiskStructServiceImpl extends ServiceImpl<RiskStructMapper, RiskStruct> implements RiskStructService {

    @Autowired
    RiskStructMapper riskStructMapper;
    @Autowired
    RelicsMapper relicsMapper;

    @Override
    public RiskStruct selectByRelicsId(Integer userId, Integer relicsId) {
        QueryWrapper<RiskStruct> wrapper = new QueryWrapper<>();
        wrapper.eq("relics_id", relicsId);
        RiskStruct riskStruct = riskStructMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(riskStruct)) throw new RequestException("文物id: " + relicsId + " 的风险数据不存在");

        /* 计算风险等级 */
        int level;
        if (riskStruct.getScore() == 0) level = 1;
        else {
            // 获取该文物在对应用户下的总分排名
            Integer rank = riskStructMapper.getRank(userId, relicsId);
            // 根据文物的所属用户id 获取所有风险数据
            List<RiskStruct> list = getAllByUserId(userId);
            // 如果总数小于等于4, 获取前4名,去掉总分为0的数据, 排序取得风险等级
            if (list.size() <= 4) {
                // 统计总分不为0的风险数据
                int count = 0;
                for (RiskStruct struct : list) {
                    if (0 != struct.getScore()) ++count;
                }
                // 若总分均不为0
                if (count == list.size()) level = count - rank + 1;
                    // 若存在总分为0
                else level = count - rank + 2;
            }
            // 否则根据排名和总数计算前百分位, 得到风险等级
            else level = ProjectUtil.countLevel(rank, list.size());
        }
        riskStruct.setLevel(level);
        return riskStruct;
    }

    @Override
    public void updateByRiskId(RiskStruct params) {
        boolean isIdExist = riskStructMapper.idExist(params.getId());
        if (!isIdExist) throw new RequestException("风险id: " + params.getId() + " 不存在");

        int score = params.getSingle() + params.getNode() + params.getOverall();
        params.setScore(score);
        riskStructMapper.updateById(params);
    }

    public List<RiskStruct> getAllByUserId(Integer userId) {
        QueryWrapper<RiskStruct> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return riskStructMapper.selectList(wrapper);
    }
}
